<template>
  <view class="">
    <view class="main">
      <view class="payInfo">
        <view class="firstLine">待支付金额（元）</view>
        <view class="num">
          <view class="price">
            <text v-if="payInfo.type === '1'">￥</text>
            {{ payInfo.price }}
            <text v-if="payInfo.type === '2'">积分</text>
          </view>
        </view>
      </view>
      <view class="payBox">
        <view>付款方式</view>
        <radio-group @change="radioChange">
          <label class="uni-list-cell uni-list-cell-pd" v-if="payInfo.type !== '2'">
            <view class="zfbbox">
              <view class="left">
                <image class="img" src="../../static/home/wx.png" mode="aspectFill" />
                <view class="textBox">
                  <view>微信</view>
                  <view>实时支付</view>
                </view>
              </view>
              <view>
                <radio value="wechat" :checked="'wechat' === payType" color="#81d8d0" />
              </view>
            </view>
          </label>
          <label class="uni-list-cell uni-list-cell-pd" v-if="payInfo.type !== '2'">
            <view class="zfbbox">
              <view class="left">
                <image class="img" src="../../static/home/zfb.png" mode="aspectFill" />

                <view class="textBox">
                  <view>支付宝</view>
                  <view>实时支付</view>
                </view>
              </view>
              <view>
                <radio value="alipay" :checked="'alipay' === payType" color="#81d8d0" />
              </view>
            </view>
          </label>
          <label class="uni-list-cell uni-list-cell-pd" v-if="payInfo.type !== '2'">
            <view class="zfbbox">
              <view class="left">
                <image class="img" src="../../static/home/ye.png" mode="aspectFill"></image>
                <view class="textBox">
                  <view>余额</view>
                  <view>当前余额：¥ {{ balance }} </view>
                </view>
              </view>
              <view>
                <radio value="balance" :checked="'balance' === payType" color="#81d8d0" />
              </view>
            </view>
          </label>
        </radio-group>
      </view>
      <view class="bottomBox">
        <u-button type="primary" @click="submitOrder" color="#81d8d0">立即支付</u-button>
      </view>
      <u-modal :show="showModal" title="请输入支付密码" showCancelButton @confirm="pay" @cancel="closeFn">
        <view class="popbox" style="width: 100%">
          <u-input v-model="payPassword" placeholder="请输入支付密码" clearable :password="isPass">
            <template #suffix>
              <u-icon name="eye-off" color="#bfbfbf" size="28" v-if="isPass" @click="isPass = false"></u-icon>
              <u-icon name="eye" color="#bfbfbf" size="28" v-else @click="isPass = true"></u-icon>
            </template>
          </u-input>
        </view>
      </u-modal>
    </view>
  </view>
</template>

<script setup lang="ts">
import { selectUserInfoByUserId } from "@/api/my";
import { balancePay, buyCarbalancePay, userPayPswCheack } from "@/api/order";
import { useStore } from "@/stores/counter";
import { onMounted, ref } from "vue";
const payInfo: any = useStore().payInfo as any;
const isPass = ref(true);
const payType = ref("balance");
const balance = ref(0);
const showModal = ref(false);
const showPopup = ref(false);
const payPassword = ref(null);
onMounted(async () => {
  const userId = uni.getStorageSync("userId");
  const res = await selectUserInfoByUserId({ userId });
  balance.value = res.amont;
});
const closeFn = () => {
  showModal.value = false;
  payPassword.value = null;
};

const radioChange = (evt: any) => {
  payType.value = evt.detail.value;
};
const submitOrder = async () => {
  console.log(payType.value);
  showPopup.value = false;
  if (payType.value === "balance" && !showModal.value) {
    showModal.value = true;
    return;
  }
  if (payType.value === "alipay") {
    const res: any = "";
    // = await payOrder({
    //   no: payInfo.no,
    //   payChannel: payType.value,
    // });

    uni.requestPayment({
      provider: "alipay",
      orderInfo: res.data,
      success(res) {
        uni.showToast({
          title: "支付成功",
          icon: "success",
        });
        setTimeout(() => {
          uni.reLaunch({
            url: `/pages/goodsDetails/goodsOrderList?status=null`,
          });
        }, 500);
      },
      fail(err) {
        console.log("fail:" + JSON.stringify(err));
        uni.redirectTo({
          url: "/pages/payPage/payFail",
        });
      },
    });
  }
};
const pay = async () => {
  await userPayPswCheack({
    userId: uni.getStorageSync("userId"),
    payPsw: payPassword.value,
  });
  if (payInfo.type === "car") {
    await buyCarbalancePay({
      userId: uni.getStorageSync("userId"),
      orderNo: payInfo.orderNo,
    });
  } else if (payInfo.type === "putong") {
    await balancePay({
      userId: uni.getStorageSync("userId"),
      orderNo: payInfo.orderNo,
    });
  }

  uni.showToast({
    title: "支付成功",
    icon: "success",
  });
  setTimeout(() => {
    uni.reLaunch({
      url: `/pages/shop/goodsDetails/goodsOrderList?status=null`,
    });
  }, 500);
};
</script>

<style lang="less" scoped>
.payInfo {
  margin-top: 30rpx;
  border-radius: 16rpx;
  text-align: center;
  padding: 32rpx 0;
  background: #fff;
  .firstLine {
    font-size: 28rpx;
    line-height: 40rpx;
  }
  .num {
    font-size: 64rpx;
    font-weight: bold;
    line-height: 82rpx;
    padding: 24rpx 0;
    text {
      font-size: 48rpx;
      font-weight: 500;
      line-height: 66rpx;
    }
  }
  .tiemLine {
    font-size: 28rpx;
    line-height: 40rpx;
    text {
      color: #02409a;
      font-weight: 500;
    }
  }
}
.payBox {
  border-radius: 8px;
  padding: 25rpx;
  margin-top: 20rpx;
  background: #fff;
  .zhTit {
    text-align: center;
    font-size: 32rpx;
    color: #000;
  }
  .zfbbox {
    display: flex;
    justify-content: space-between;
    margin: 30rpx 0;
    align-items: center;
    padding-bottom: 20rpx;
    .left {
      display: flex;
      .img {
        width: 56rpx;
        height: 56rpx;
      }
      .iconBox {
        position: relative;
        margin-right: 14rpx;
        .img,
        .icon {
          width: 38rpx;
          height: 38rpx;
          position: absolute;
          left: 20rpx !important;
          top: 20rpx !important;
        }
        .icon {
          left: 0;
          top: 0;
        }
      }
      .textBox {
        font-size: 30rpx;
        margin-left: 30rpx;
        & > view {
          &:last-child {
            color: #a6a9af;
            font-size: 24rpx;
          }
        }
      }
    }
  }
}
.popbox {
  width: 600rpx;
  padding: 25rpx;
  text-align: center;
}
.zhzf {
  display: flex;
  color: #5f6368;
  align-items: center;
  font-size: 26rpx;
}
</style>
